﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Reporting;
using DataAccess;
using BusinessObjects;
using Toolkit;
using System.Globalization;

namespace BusinessLogic {
  /// <summary>
  /// Business Logic Layer for reports
  /// </summary>
  public class ReportBll {

    ReportDal dal = new ReportDal( );

    /// <summary>
    /// Gets the childfiche report data.
    /// </summary>
    /// <param name="children">The children.</param>
    /// <returns></returns>
    public ChildficheData GetChildficheReportData( Children children ) {
      return dal.GetChildficheReportData( children );
    }

    /// <summary>
    /// Gets the childfiche detail report data.
    /// </summary>
    /// <param name="children">The children.</param>
    /// <returns></returns>
    public ChildficheDetailData GetChildficheDetailReportData( Children children ) {
      return dal.GetChildficheDetailReportData( children );
    }

    /// <summary>
    /// Gets the month invoice report data.
    /// </summary>
    /// <param name="month">The month.</param>
    /// <param name="year">The year.</param>
    /// <returns></returns>
    public InvoiceMonthReportData GetMonthInvoiceReportData( int month, int year ) {

      List<int> childIds = new List<int>( );
      int daysInMonth = DateTime.DaysInMonth( year, month );
      DateTime fromDate = new DateTime( year, month, 1, 0, 0, 0 );
      DateTime toDate = new DateTime( year, month, daysInMonth, 23, 59, 59 );

      InvoiceMonthReportData data = dal.GetMonthInvoice( fromDate, toDate );

      foreach ( InvoiceMonthReportData.InvoiceMonthRow row in data.InvoiceMonth ) {
        if ( !childIds.Contains( row.ChildId ) ) {
          childIds.Add( row.ChildId );
        }
        if ( !row.IsChildArrivalDateTimeNull( ) ) {
          row.Day = row.ChildArrivalDateTime.Date;

          TimeSpan minTime = DateTimeTools.Convert.ToTimeSpan( row.ChildMinTime );
          TimeSpan maxHalfTime = minTime.Add( new TimeSpan( 4, 0, 0 ) );
          TimeSpan maxTime = DateTimeTools.Convert.ToTimeSpan( row.ChildMaxTime );

          TimeSpan arrivalTime = row.ChildArrivalDateTime.TimeOfDay;
          TimeSpan departureTime = row.ChildDepartureDateTime.TimeOfDay;

          TimeSpan overTime = new TimeSpan( );
          bool hasOverTime = true;

          if ( arrivalTime < minTime ) {
            overTime = minTime.Subtract( arrivalTime );
            hasOverTime = true;
          }
          if ( departureTime > maxTime ) {
            overTime = overTime.Add( departureTime.Subtract( maxTime ) );
            hasOverTime = true;
          }
          if ( hasOverTime ) {
            if ( overTime.Minutes > 30 ) {
              overTime = new TimeSpan( overTime.Hours + 1, 0, 0 );
            } else {
              overTime = new TimeSpan( overTime.Hours, 0, 0 );
            }
            row.OverTime = overTime.ToString( ).Substring( 0, 5 );
            row.OvertimePrice = row.BasicPrice * overTime.Hours * 0.5m;
          }

          if ( departureTime > maxHalfTime ) {
            //full day
            row.FullDayPrice = row.BasicPrice;
          } else {
            //half day
            row.HalfDayPrice = row.BasicPrice * 0.5m;
          }


        }
      }

      foreach ( int id in childIds ) {
        for ( int i = 1; i < daysInMonth + 1; i++ ) {
          DateTime day = new DateTime( year, month, i );
          string filter = string.Format( CultureInfo.CurrentCulture, "ChildId = {0} And Day = '#{1}#'",id, day );
          InvoiceMonthReportData.InvoiceMonthRow[ ] rows = ( InvoiceMonthReportData.InvoiceMonthRow[ ] ) data.InvoiceMonth.Select( filter );
          if ( rows.Length==0 ) {
            InvoiceMonthReportData.InvoiceMonthRow emptyRow = data.InvoiceMonth.NewInvoiceMonthRow( );
            emptyRow.ChildId = id;
            emptyRow.Day = day;
            data.InvoiceMonth.AddInvoiceMonthRow( emptyRow );
          }
        }
      }

      return data;
    }

  }
}
